<template>
  <div class="file-to-text-container">
    <!-- 头部区域 -->
    <el-page-header
      @back="goBack"
      :icon="ArrowLeft"
      class="file-to-text-header"
    >
    <template #content>
      <span class="text-large font-900 mr-3"> 导入音视频转文字 </span>
      </template>
    </el-page-header>

    <div class="upload-card">
      <!-- 上传图标 -->
      <div class="upload-icon-wrapper">
        <div class="upload-icon">
          <img src="@/assets/images/home/uploadIcon.png" alt="upload" />
        </div>
      </div>

      <!-- 标题 -->
      <h2 class="upload-title">上传本地音视频</h2>

      <!-- 支持格式提示 -->
      <p class="upload-subtitle">支持 mp4/mp3/wav/m4a 等格式</p>

      <!-- 上传按钮 -->
      <button class="upload-button" @click="handleSelectFile">
        选择文件
      </button>

      <!-- 隐藏的文件输入 -->
      <input
        ref="fileInputRef"
        type="file"
        accept="audio/*,video/*,.mp3,.mp4,.wav,.m4a"
        style="display: none"
        @change="handleFileChange"
      />
    </div>
  </div>
</template>

<script setup lang='ts'>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const goBack = () => {
  router.back()
}

const fileInputRef = ref<HTMLInputElement | null>(null)
const selectedFile = ref<File | null>(null)

// 点击选择文件按钮
const handleSelectFile = () => {
  fileInputRef.value?.click()
}

// 处理文件选择
const handleFileChange = (event: Event) => {
  const target = event.target as HTMLInputElement
  const file = target.files?.[0]
  
  if (!file) return
  
  // 验证文件类型
  const validTypes = ['audio/mpeg', 'audio/mp3', 'audio/wav', 'audio/x-m4a', 'video/mp4', 'audio/mp4']
  const validExtensions = ['.mp3', '.mp4', '.wav', '.m4a']
  
  const isValidType = validTypes.some(type => file.type.includes(type)) || 
                      validExtensions.some(ext => file.name.toLowerCase().endsWith(ext))
  
  if (!isValidType) {
    ElMessage.error('请上传 mp4/mp3/wav/m4a 格式的文件')
    return
  }
  
  // 验证文件大小（可选，例如限制500MB）
  const maxSize = 500 * 1024 * 1024 // 500MB
  if (file.size > maxSize) {
    ElMessage.error('文件大小不能超过 500MB')
    return
  }
  
  selectedFile.value = file
  ElMessage.success(`已选择文件: ${file.name}`)
  
  // TODO: 在这里处理文件上传逻辑
  console.log('选择的文件:', file)
  
  // 重置输入，允许重复选择同一文件
  if (target) {
    target.value = ''
  }
}
</script>

<style lang='scss' scoped>
.file-to-text-container {
  min-height: 100vh;
  background: linear-gradient(180deg, #FFFFFF 0%, #ECF3FF 100%);
  padding: 20px;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.file-to-text-header {
  flex-shrink: 0;
  :deep(.el-page-header__content) {
    font-size: 16px;
    font-weight: 600;
  }
}

.upload-card {
  background: #ffffff;
  border-radius: 24px;
  padding: 60px 40px;
  max-width: 500px;
  width: 100%;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
  text-align: center;
  margin: auto;
  box-sizing: border-box;
}

.upload-icon-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 32px;
}

.upload-icon {
  width: 120px;
  height: 120px;
  background: linear-gradient(135deg, #e8ebff 0%, #f0f2ff 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  img {
    width: 50%;
  }
}

.upload-title {
  font-size: 22px;
  font-weight: 600;
  color: #1a1a1a;
  margin: 0 0 16px 0;
  letter-spacing: -0.5px;
}

.upload-subtitle {
  font-size: 15px;
  color: #999999;
  margin-bottom: 30px;
  font-weight: 400;
}

.upload-button {
  width: 100%;
  height: 56px;
  background: linear-gradient(135deg, #4097FF 0%, #3D8AFF 100%);
  border: none;
  border-radius: 16px;
  color: #ffffff;
  font-size: 18px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 16px rgba(91, 109, 232, 0.3);
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(91, 109, 232, 0.4);
  }
  
  &:active {
    transform: translateY(0);
  }
}

// 响应式设计
@media (max-width: 768px) {
  .upload-card {
    padding: 40px 24px;
    border-radius: 20px;
  }
  
  .upload-icon {
    width: 100px;
    height: 100px;
    
    svg {
      width: 40px;
      height: 40px;
    }
  }
  
  .upload-title {
    font-size: 24px;
  }
  
  .upload-subtitle {
    font-size: 14px;
  }
  
  .upload-button {
    height: 52px;
    font-size: 16px;
  }
  
  .upload-notice span {
    font-size: 13px;
  }
}
</style>